package com.zmn.plat.admin.controller.warranty.content;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zmn.common.dto2.ResponseDTO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.model.entity.warranty.content.WarrantyContent;

/**
 * 保修内容配置
 *
 * @author linfeng
 * @since 2020-06-08 10:37:02
 */
@RestController
@RequestMapping("/warranty/content/")
public class WarrantyContentModifyController extends WarrantyContentBaseController {

    /**
     * 新增修改
     *
     * @param warrantyContent
     * @author linfeng
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping(value = "addEdit")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody WarrantyContent warrantyContent) {
        Date now = DateUtil.getNow();

        Integer warrantyContentId = warrantyContent.getWarrantyContentId();

        try {
            if (warrantyContentId == null || warrantyContentId == 0) {
                warrantyContent.setCreater(getMcStaffRealName());
                warrantyContent.setCreateTime(now);
                warrantyContent.setUpdater(getMcStaffRealName());
                warrantyContent.setUpdateTime(now);
                warrantyContent.setStatus(GlobalConsts.YES);
                warrantyContentService.insert(warrantyContent);
            } else {
                warrantyContent.setUpdater(getMcStaffRealName());
                warrantyContent.setUpdateTime(now);
                warrantyContentService.updateByKey(warrantyContent);
            }

            return ResponseDTO.success(GlobalConsts.YES,"操作成功！");
        } catch (org.springframework.dao.DuplicateKeyException ex) {
            logger.error("同一服务分类和产品一级分类下的保修内容不能重复！", warrantyContent.getContent());
            return ResponseDTO.fail("同一服务分类和产品一级分类下的保修内容不能重复！");
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改保修内容配置失败,warrantyContent:[%s],ex:[%s]", JSONObject.toJSONString(warrantyContent),
                ex.getMessage()));
            return ResponseDTO.fail("操作失败！");
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     * @author linfeng
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping(value = "updateSort")
    public ResponseDTO updateSort(HttpServletRequest request, @RequestParam(value = "id", defaultValue = "0") Integer id,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (id == 0) {
            return ResponseDTO.fail("请选择要更改排序的数据！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置排序值！");
        }

        warrantyContentService.updateSortByKey(id, sort, getMcStaffRealName());
        return ResponseDTO.success(GlobalConsts.SUCCESS_MSG);
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @author linfeng
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping(value = "updateStatus")
    public ResponseDTO updateStatus(HttpServletRequest request, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") Integer status) {
        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的数据！");
        }
        if (status == 0) {
            return ResponseDTO.fail("请设置状态！");
        }

        List<Integer> arrayIds = StringUtil.convertStrsToList(ids);

        warrantyContentBService.updateStatusBatchAndDeleteWarrantyInfoByKey(arrayIds, status, getMcStaffRealName());
        return ResponseDTO.success(GlobalConsts.SUCCESS_MSG);
    }

}
